import 'package:flutter/material.dart';
import 'package:flutter_base_study/dialog/customdialog.dart';
import 'package:fluttertoast/fluttertoast.dart';

class DialogPage extends StatefulWidget {
  const DialogPage({super.key});

  @override
  State<DialogPage> createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  void _alertDialog() async {
    var result = await showDialog(
        //表示点击周边灰色背景的时候dialog是否消失
        barrierDismissible: true,
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('AlertDialog标题'),
            content: Text('AlertDialog内容'),
            actions: [
              GestureDetector(
                child: Text('取消', style: TextStyle(fontSize: 15)),
                onTap: () {
                  Navigator.of(context).pop('取消');
                },
              ),
              TextButton(
                  onPressed: () {
                    Navigator.of(context).pop('确定');
                  },
                  child: Text('确定'))
            ],
          );
        });

    ///如何获取dialog点击的值，   async + await
    print('AlertDialog result=' + result);
  }

  void _simpleDialog() async {
    var result = await showDialog(
        //表示点击周边灰色背景的时候dialog是否消失
        barrierDismissible: true,
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text('SimpleDialog标题'),
            children: [
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, '科比');
                },
                child: Text('科比'),
              ),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, '麦迪');
                },
                child: Text('麦迪'),
              ),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, '詹姆斯');
                },
                child: Text('詹姆斯'),
              )
            ],
          );
        });
    print('simpledialog result=' + result);
  }

  void _showModalBottomSheet() async {
    var result = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return SizedBox(
            height: 200,
            child: Column(
              children: [
                ListTile(
                  title: Text('NBA'),
                  onTap: () {
                    Navigator.pop(context, 'NBA');
                  },
                ),
                Divider(),
                ListTile(
                  title: Text('CBA'),
                  onTap: () {
                    Navigator.pop(context, 'CBA');
                  },
                ),
                Divider(),
                ListTile(
                  title: Text('WNBA'),
                  onTap: () {
                    Navigator.pop(context, 'WNBA');
                  },
                ),
              ],
            ),
          );
        });
    print('_actionSheet result=' + result);
  }

  void _flutterToast() {
    Fluttertoast.showToast(
        msg: "This is Center Short Toast",
        //只针对安卓平台
        toastLength: Toast.LENGTH_LONG,
        gravity: ToastGravity.CENTER,
        //提示时间，针对ios和web
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0);
  }

  void _myDialog() async{
    var result = showDialog(
        barrierDismissible: true, //表示点击灰色背景的时候是否消失弹出框
        context: context,
        builder: (context) {
          return CustomDialog(
            title: "title",
            content: 'content',
            onClosed: () {
              Navigator.pop(context,"close");
            },
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dialog"),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
                onPressed: () {
                  _alertDialog();
                },
                child: Text('alert弹出框-AlertDialog')),
            ElevatedButton(
                onPressed: () {
                  _simpleDialog();
                },
                child: Text('select弹出框-SimpleDialog')),
            ElevatedButton(
                onPressed: () {
                  _showModalBottomSheet();
                },
                child: Text('showModalBottomSheet 底部弹出框')),
            ElevatedButton(
                onPressed: () {
                  _flutterToast();
                },
                child: Text('FlutterToast')),
            ElevatedButton(
                onPressed: () {
                  _myDialog();
                },
                child: Text('自定义Dialog')),
          ],
        ),
      ),
    );
  }
}
